#include "ZhouBian_ui_logic.h"
#include "product_detail_widget.h"
productgridwidget::productgridwidget( QWidget *parent)
    : QWidget(parent), currentPage(0), eventWidgets(new QList<ProductWidget *>())
{
    // Set up main layout and pagination layout
    gridLayout = new QVBoxLayout(this);
    productListLayout = new QVBoxLayout();
    paginationLayout = new QHBoxLayout();

    QPushButton *prevButton = new QPushButton("上一页", this);
    QPushButton *nextButton = new QPushButton("下一页", this);

    paginationLayout->addWidget(prevButton);
    paginationLayout->addStretch();
    paginationLayout->addWidget(nextButton);

    connect(prevButton, &QPushButton::clicked, this, &productgridwidget::goToPreviousPage);
    connect(nextButton, &QPushButton::clicked, this, &productgridwidget::goToNextPage);

    gridLayout->addLayout(productListLayout);
    gridLayout->addLayout(paginationLayout);
    setLayout(gridLayout);
}

productgridwidget::~productgridwidget()
{
    qDeleteAll(*eventWidgets);
    delete eventWidgets;
}

// Populate product widgets, creating them if they don't already exist
void productgridwidget::populateProductWidgets()
{
    qDeleteAll(*eventWidgets);
    eventWidgets->clear();

    if (eventWidgets->isEmpty()) {
        for (int i = 0; i < zhoubianProductList.size(); ++i)
        {
            ProductWidget *productWidget = new ProductWidget(this);
            eventWidgets->append(productWidget);
            connect(productWidget, &ProductWidget::productClicked, this, &productgridwidget::showProductDetail);
        }
    }
    updatePage(); // Load the first page
}

void productgridwidget::setEventData(const QPixmap &image,
                                     const QString &title,
                                     const QString &price,
                                     const QString &description,
                                     int productId,
                                     int index)
{
    if (index < eventWidgets->size())
    {
        ProductWidget *productWidget = eventWidgets->at(index);
        productWidget->setEvent(image, title, price, description,productId);
    }
}

void productgridwidget::updatePage()
{
    // Clear the layout and remove widgets without deleting them
    QLayoutItem *child;
    while ((child = productListLayout->takeAt(0)) != nullptr)
    {
        child->widget()->setParent(nullptr);
        delete child; // Remove layout item, not the widget
    }

    int startIndex = currentPage * itemsPerPage;
    int endIndex = qMin(startIndex + itemsPerPage, zhoubianProductList.size());

    for (int i = startIndex; i < endIndex; ++i)
    {
        if ((*eventWidgets)[i] != nullptr) {
            productListLayout->addWidget((*eventWidgets)[i]);
        } else {
            qDebug() << "Warning: eventWidgets[" << i << "] is a null pointer.";
        }
    }

    updatePagination();
}

void productgridwidget::updatePagination()
{
    bool hasNextPage = (currentPage + 1) * itemsPerPage < zhoubianProductList.size();
    bool hasPreviousPage = currentPage > 0;

    paginationLayout->itemAt(0)->widget()->setEnabled(hasPreviousPage); // Previous button
    paginationLayout->itemAt(2)->widget()->setEnabled(hasNextPage);     // Next button
}

/*void productgridwidget::handleBuyButtonClicked(const QString &title, const QString &price, int productId)
{
    qDebug() << "周边商品已购买: " << title << " 价格: " << price << " 商品ID: " << productId;
    shoppingCartLogic->addProductRow(title, price, productId, 1);
}*/

void productgridwidget::goToNextPage()
{
    if ((currentPage + 1) * itemsPerPage < zhoubianProductList.size())
    {
        ++currentPage;
        updatePage();
    }
}

void productgridwidget::goToPreviousPage()
{
    if (currentPage > 0)
    {
        --currentPage;
        updatePage();
    }
}

void productgridwidget::showProductDetail(const QPixmap &image, const QString &title,
                                        const QString &description,
                                        const QString &price, int productId)
{
    ProductDetailWidget *detailWidget = new ProductDetailWidget();
    detailWidget->setupProductInfo(image, title, price, description, productId);

    detailWidget->setWindowTitle("Product Details");
    detailWidget->setAttribute(Qt::WA_DeleteOnClose); // Ensure it gets deleted when closed
    detailWidget->show();
}
